package com.example.danangtravel.ATM;

import java.util.ArrayList;

import com.exam.danangtranel.filed.ATM;
import com.example.danangtravel.R;
import com.example.danangtravel.checknetwork;
import com.example.danangtravel.map.RoutePath;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ATM_Adapter extends BaseExpandableListAdapter {

	private LayoutInflater inflater;
	private ArrayList<ATM_Titlee> atmtitle;
	public checknetwork check;

	public ATM_Adapter(Context context, ArrayList<ATM_Titlee> atmtitle) {
		this.atmtitle = atmtitle;
		inflater = LayoutInflater.from(context);
	}

	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return atmtitle.get(groupPosition).getChild().get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.list_item_child, parent,
					false);
		}
		TextView textView = (TextView) convertView
				.findViewById(R.id.list_item_text_child);
		textView.setText(atmtitle.get(groupPosition).getChild()
				.get(childPosition).getAddress().toString());

		// return the entire view
		convertView.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				check = new checknetwork(v.getContext());
				if (check.isOnline()) {
					ATM atm = new ATM();
					atm = atmtitle.get(groupPosition).getChild()
							.get(childPosition);
					Intent i = new Intent(v.getContext(), RoutePath.class);
					i.putExtra("latitude", atm.getLongitude()); // bi dao?
																// nguoc...
					i.putExtra("longitude", atm.getLatitude()); // bi dao?
																// nguoc...
					v.getContext().startActivity(i);
				} else
					Toast.makeText(v.getContext(), "Không có kết nối !", 1000)
							.show();
			}
		});
		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return atmtitle.get(groupPosition).getChild().size();
	}

	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return atmtitle.get(groupPosition).getTitle();
	}

	public int getGroupCount() {
		// TODO Auto-generated method stub
		return atmtitle.size();
	}

	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.list_item_parent, parent,
					false);
		}
		TextView textView = (TextView) convertView
				.findViewById(R.id.list_item_text_view);
		textView.setText(getGroup(groupPosition).toString());
		return convertView;
	}

	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

}
